//
//  BeforePublishVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/7/2.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "BeforePublishVC.h"
#import "OwnerCellTableViewCell.h"
#import "MJRefresh.h"
#import "PdcDetailVC.h"
#import "WaitingForRltCell.h"
#import "NoDataView.h"
#define HISTORY_URL @"/api/ApiGoods/getRevealHistory"
@interface BeforePublishVC ()<UITableViewDataSource,UITableViewDelegate>
{
    //往期列表
    UITableView *_tableView;
    //数据源
    NSMutableArray *_data;
    NSInteger _currentPage;
}
@end

@implementation BeforePublishVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _data = [NSMutableArray array];
    _currentPage = 1;
    [self loadCustomNaviBar];
    [self loadTableView];
    [self startRequestForData];
}
//加载导航条
- (void)loadCustomNaviBar
{
    UIView *naviBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WYMWITH, 20+44)];
    naviBar.backgroundColor = QBB_RED_COLOR;
    //返回按钮
    PublicBackBtn *backBtn = [[PublicBackBtn alloc]initWithVC:self andBackType:pop Img:[UIImage imageNamed:@"icon_back_white_press"]];
    [naviBar addSubview:backBtn];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 25)];
    titleLab.center = CGPointMake(WYMWITH/2, 42);
    titleLab.text = @"往期揭晓";
    titleLab.font = [UIFont systemFontOfSize:TITLE_SIZE];
    titleLab.textAlignment = NSTextAlignmentCenter;
    titleLab.textColor = [UIColor whiteColor];
    //分割线
    UILabel *fenge = [[UILabel alloc]initWithFrame:CGRectMake(0, 63.5, WYMWITH, 0.5)];
    fenge.backgroundColor = [UIColor lightGrayColor];
    [naviBar addSubview:fenge];
    [naviBar addSubview:titleLab];
    [self.view addSubview:naviBar];
}
//加载表格
- (void)loadTableView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, WYMWITH, WYMHEIGHT - 64)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    //注册cell
    [_tableView registerNib:[UINib nibWithNibName:@"OwnerCellTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"WaitingForRltCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [self setupRefresh];
}
/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    //    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // dateKey用于存储刷新时间，可以保证不同界面拥有不同的刷新时间
    [_tableView addHeaderWithTarget:self action:@selector(headerRereshing) dateKey:@"table"];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    
    
    _currentPage = 1;
    [_data removeAllObjects];
    [self startRequestForData];
}

- (void)footerRereshing
{
    _currentPage++;
    [self startRequestForData];
}
//启动数据请求
- (void)startRequestForData
{
    [WYPublic addHudToWindow];
    NSDictionary *params = @{@"id":self.pdc_id,@"num":@"20",@"page":[NSString stringWithFormat:@"%ld",(unsigned long)_currentPage]};
    __weak typeof(_data) _weakData;
    __weak typeof(self)  __weakSelf;
    __weak typeof (_tableView) __weakTableView;
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,HISTORY_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSArray *owners = dic[@"data"];
        for (NSDictionary *owner in owners) {
            OwnerModel *model = [[OwnerModel alloc]init];
            model.owner_img_header = owner[@"img_url"];
            if ([owner[@"addr"] class] == [NSNull class] || owner[@"addr"] == nil) {
                model.owner_address = @"地址待定";
            }else{
                model.owner_address = owner[@"addr"];
            }
            model.owner_id = owner[@"id"];
            model.owner_name = owner[@"username"];
            model.owner_num = owner[@"q_user_code"];
            model.owner_qishu = owner[@"qishu"];
            model.owner_sid = owner[@"sid"];
            model.owner_time = owner[@"q_end_time"];
            NSNumber *n_t = owner[@"go_total"];
            model.owner_totle = [NSString stringWithFormat:@"%d",[n_t intValue]];
            model.owner_uid = owner[@"uid"];
            model.owner_statu = owner[@"status"];
            [_data addObject:model];
        }
        if (_data.count == 0) {
            [self addNODataView];
        }
        [_tableView reloadData];
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
         [WYPublic removeHudToWindow];
    } failedBlock:^{
        [_tableView headerEndRefreshing];
        [_tableView footerEndRefreshing];
         [WYPublic removeHudToWindow];
    } needLog:NO];
}
//没有数据默认填充的控件
- (void)addNODataView{
    NoDataView *nodateView = [[[NSBundle mainBundle]loadNibNamed:@"NoDataView" owner:self options:nil]lastObject];
    nodateView.frame = CGRectMake(0, 64, WYMWITH, nodateView.frame.size.height);
    nodateView.title = @"小伙伴们还没有中奖记录哦!";
    [self.view addSubview:nodateView];
}
#pragma mark -- 表格协议函数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   
    UITableViewCell *cell = nil;
//    if(!cell){
//        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Defaultcell"];
//    }

    
    if (_data.count > 0) {
        OwnerModel *model = _data[indexPath.row];
        if ([model.owner_statu intValue] == 2 || [model.owner_statu intValue] == 4 || [model.owner_statu intValue] == 5) {
            WaitingForRltCell *wcell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
            [wcell fillCellWithModel:model];
            cell = wcell;
        }else{
            OwnerCellTableViewCell *ocell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
            [ocell fillCellWithModel:model];
            cell = ocell;
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_data.count > 0) {
        OwnerModel *model = _data[indexPath.row];
        if ([model.owner_statu intValue] == 2 || [model.owner_statu intValue] == 4 || [model.owner_statu intValue] == 5) {
            return 40.0;
        }
    }
    return 150.0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    //保存至兴趣记录
    OwnerModel *currentModel = _data[indexPath.row];
//    WYShopCarModel *model = [[WYShopCarModel alloc]init];
//    model.car_id = currentModel.owner_sid;
//    model.name = currentModel.ow;
//    model.imgPath = currentModel.pdc_img_path;
//    model.count = 1;
//    [[WYDataBase sharedDataBase]insertPdcToIntrestCarWithModel:model];
//    
    
    //跳往商品详情
    PdcDetailVC *detailVC = [[PdcDetailVC alloc]init];
    detailVC.pdc_id = currentModel.owner_id;
    [[WYMainTabBarVC shareMainTaBarVC].navigationController pushViewController:detailVC animated:YES];
}



-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)dealloc{
    
    NSLog(@"BeforePublishVC");
}

@end
